﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Tunneler {
    class PlayerBase : IGameObject {
        GraphicsDevice graphicsDevice;

        PlayerType playerType;

        Texture2D texture;
        Color[] textureData;

        float rotation = 0f;
        Vector2 origin;
        Vector2 position;

        public float Rotation { get { return rotation; } }
        public Texture2D Texture { get { return texture; } }
        public Color[] TextureData { get { return textureData; } }
        public float TextureScale { get { return 1f; } }
        public Vector2 Origin { get { return origin; } }
        public Vector2 Position { get { return position; } set { position = value; } }

        public PlayerBase(GraphicsDevice graphicdDevice, PlayerType playerType) {
            this.graphicsDevice = graphicdDevice;
            this.playerType = playerType;
        }

        public void Update(GameTime gameTime) {
            // Base doesn't do anything right now
        }

        public void TouchedBy(IGameObject other) {
            // Base doesn't react to outside stimuly
        }

        public void TouchedBy(Terrain terrain) {
            // Base isn't in contact with terrain aside from its creation
            // Right then terrain is cleared and this method should never be called again
        }

        public void LoadContent(ContentManager content) {
            GenerateTextureAndData();
            origin = new Vector2(texture.Width / 2, texture.Height / 2);
        }

        public void Draw(SpriteBatch spriteBatch) {
            spriteBatch.Draw(texture, position, null, Color.White, rotation, origin, TextureScale, SpriteEffects.None, 0);
        }

        private void GenerateTextureAndData() {
            int width = 150;
            int height = 150;
            texture = new Texture2D(graphicsDevice, width, height, false, SurfaceFormat.Color);
            textureData = new Color[width * height];

            Color color = Color.Blue;
            if (playerType == PlayerType.Red) {
                color = Color.Red;
            }

            for (int i = 0; i < width; ++i) {
                for (int j = 0; j < height; ++j) {
                    if ((i <= 5 || (i > width - 5 && i <= width)) || (j <= 5 || (j <= height && j > height - 5))) {
                        textureData[i + j * width] = color;
                    }
                }
            }
            int doorWidth = width / 3;
            for (int i = 0; i < doorWidth; ++i) {
                for (int j = 0; j <= 5; ++j) {
                    // Top Door
                    textureData[(i + width / 2 - doorWidth / 2) + j * width] = new Color();

                    // Bot Door
                    textureData[(i + width / 2 - doorWidth / 2) + (height - j - 1) * width] = new Color();
                }
            }
            texture.SetData(textureData);
        }
    }
}
